package com.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.me.bean.Xy;
import com.me.blood.R;
import com.me.blood.activity.XtHistory;
import com.me.blood.activity.XyHistory;
import com.me.blood.databinding.XtHisDayBinding;
import com.me.blood.databinding.XtOneItemBinding;
import com.me.blood.databinding.XyOneItemBinding;
import com.me.utils.Utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

public class XyHisAdapter extends RecyclerView.Adapter {

    List list;
    XyHistory ac;

    public XyHisAdapter(XyHistory ac, List list) {
        this.list = list;
        this.ac = ac;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            XtHisDayBinding binding = XtHisDayBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new DayHolder(binding);
        } else {
            XyOneItemBinding binding = XyOneItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ItemHolder(binding, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof DayHolder) {
            ((DayHolder) holder).binding.tv.setText(list.get(position).toString());
        } else if (holder instanceof ItemHolder) {
            ((ItemHolder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position) instanceof Xy) {
            boolean first = (!(list.get(position - 1) instanceof Xy));
            boolean end;
            if (position == list.size() - 1) {
                end = true;
            } else {
                end = (!(list.get(position + 1) instanceof Xy));
            }
            if (first && end) {
                return 1;
            } else if (first) {
                return 2;
            } else if (end) {
                return 3;
            }
            return 4;
        } else {
            return 0;
        }
    }

    class DayHolder extends RecyclerView.ViewHolder {
        XtHisDayBinding binding;

        DayHolder(XtHisDayBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    private void remove(int p, int type) {
        if (type == 1) {
            list.remove(p);
            list.remove(p - 1);
            notifyItemRangeRemoved(p - 1, 2);
        } else if (type == 2) {
            list.remove(p);
            notifyItemRemoved(p);
            notifyItemChanged(p);
        } else if (type == 3) {
            list.remove(p);
            notifyItemRemoved(p);
            notifyItemChanged(p - 1);
        } else if (type == 4) {
            list.remove(p);
            notifyItemRemoved(p);
        }
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        XyOneItemBinding binding;
        int type;

        public long delete(int p) {
            long id;
            Object obj = list.get(p);
            if (obj instanceof Xy) {
                id = ((Xy) obj).id;
                remove(p, type);
            } else {
                id = -1;
            }
            return id;
        }

        ItemHolder(XyOneItemBinding binding, int type) {
            super(binding.getRoot());
            this.binding = binding;
            this.type = type;
            if (type == 2) {
                binding.getRoot().setBackgroundResource(R.drawable.white_top_round_bg);
            } else if (type == 3) {
                binding.getRoot().setBackgroundResource(R.drawable.white_bottom_round_bg);
                binding.line.setVisibility(View.INVISIBLE);
            } else if (type == 4) {
                binding.getRoot().setBackgroundColor(Color.WHITE);
            } else {
                binding.line.setVisibility(View.INVISIBLE);
            }
        }

        void bind(int p) {
            Xy item = (Xy) list.get(p);
            binding.value.setText(Utils.getValue(item));
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
            String createDate = formatter.format(item.time);
            binding.time.setText(createDate);
            binding.state.setText(Utils.getState(item, 1));
            binding.state.setTextColor(Utils.getStateColor(item, 1));
            binding.type.setText(Utils.getState(item, 2));
            binding.type.setTextColor(Utils.getStateColor(item, 2));
            itemView.setOnClickListener(view -> {
                ac.jump(getBindingAdapterPosition(), item);
            });
        }
    }
}
